
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 登录面板
 */
public class LoginPanel extends JPanel implements ActionListener {
    private JButton loginBtn;
    private JButton register;
    private  JTextField denglu;
    private JPasswordField awd;
    private Atmtest frame;
    private int a =3;
    public LoginPanel(Atmtest frame){
        this.frame = frame;
        this.setLayout(null);

        denglu =new JTextField();
        denglu.setBounds(180,500,200,30);
        this.add(denglu);

        awd =new JPasswordField();
        awd.setBounds(180,550,200,30);
        this.add(awd);

        JLabel titleLabel = new JLabel("账号");
        titleLabel.setBounds(150,500,30,30);
        this.add(titleLabel);

        JLabel titleLabel1 = new JLabel("密码");
        titleLabel1.setBounds(150,550,30,30);
        this.add(titleLabel1);

        loginBtn = new JButton(Util.getIcon("/img/buttonLand.jpg"));
        loginBtn.setBounds(280,600,100,30);
        this.add(loginBtn);
        loginBtn.addActionListener(this);

        register = new JButton(Util.getIcon("/img/buttonLogin.jpg"));
        register.setBounds(280,640,100,30);
        this.add(register);
        register.addActionListener(this);
        this.setOpaque(false);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == loginBtn){
                    for (int j = 0; j <frame.getUsers().size(); j++) {
                        if (denglu.getText().equals(frame.getUsers().get(j).getId()) && String.valueOf(awd.getPassword()).equals(frame.getUsers().get(j).getAwd())) {
                            frame.switchPanel("menu");
                            frame.setID(j);
                            frame.setUser(frame.getUsers().get(j));
                            break;
                        }
                    }
                    denglu.setText("");
        }
        if(e.getSource() == register){
            frame.switchPanel("logon");
        }
    }

}
